<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: page 
* Author			: 宁秀娟
* Create Time		: 2012-05-16
* Modify Time		: 2012-05-16 
* Modify Profile	:  
******************************************************/
class page_class extends Admin_Controller {

	/******************************************************
	 * Profile			: 列表
	* Author			: 宁秀娟
	* Create Time		: 2012-05-16
	* Modify Time		: 2012-05-16
	* Modify Profile	:
	******************************************************/
	public function index()
	{
		$page_class = new page_class_model();
		// print_r($page_class);exit;
		if(isset($_GET['id']))
		{
			$page_class = $this->page_class_manager->get($this->input->get('id')); 
		}
		if($this->is_postback())
		{
			$this->load->library('form_validation');
	
			if ($this->form_validation->run('page_class') == TRUE)
			{
				$post_page_class = $this->filter_post();
				$id = $this->input->get('id');
				if(empty($id))
				{
					$id = $this->page_class_manager->insert($post_page_class);
				}
				else
				{
					$this->page_class_manager->update($post_page_class,array('id'=>$id)) ; 
				}
				redirect(SELF."/page_class/index?id=$id") ;
			}
		}
		$page_classes = $this->page_class_manager->get_root_list();
		$page_class->page_classes = $page_classes ;
		// print_r($page_class);exit;
		
		$page_class->part_templates =  $this->part_templates(); 
		$this->load->view("admin/page_class/v_index",$page_class);
	}
	
	private function part_templates()
	{
		$page_view_folder = APPPATH."views/".$this->template."/front/page";
		// echo $page_view_folder;exit ;
		$d = dir($page_view_folder);
		$part_templates = Array();
		while (false !== ($f = $d->read()))
		{
			if(preg_match_all("/^v_(.*?)_index.php$/i", $f,$m))
			{
				$part_templates[] =  $m[1][0];
			}
		}
		return $part_templates;
	}
	
	/******************************************************
	* Profile		: 编辑
	* Author		: 宁秀娟
	* Create Time		: 2012-05-16 
	* Modify Time		: 2012-05-16
	* Modify Profile	:  
	******************************************************/
	public function edit($id = null)
	{
		$id = ($id == null)?$this->input->get("id"):$id;
		
		if($id == null) // 添加
		{
			$page_class = $this->build_class('page_class');
		}
		else
		{
			$page_class = $this->page_class_manager->get($id); 
		}
			
		if($this->is_postback())
		{
			$this->load->library('form_validation');
	
			if ($this->form_validation->run('page_class') == TRUE)
			{
				$page_class = $this->filter_post($_POST); 
				
				// print_r($page_class);exit;
	
				if($id == null)  // 添加
				{
					// $page_class['contribute_time'] = Date('Y-m-d');  
					$id=$this->page_class_manager->insert($page_class);  
				}
				else 
				{
					$this->page_class_manager->update($page_class,array('id'=>$id));
				}
				if(!empty($_GET['returnurl']))
				{
					$this->goto_url(urldecode($this->input->get('returnurl')."&key_id=$id")) ;
					exit;
				}
				redirect("admin.php/page_class/index?tab=system_manager") ;
				// $this->goto_last_index_page();
			}
		}
		$page_class->part_templates =  $this->part_templates();
		// print_r($page_class);exit; 
		$this->load->view("admin/page_class/v_form",$page_class);
	}


	/******************************************************
	* Profile		: 删除
	* Author		: 宁秀娟
	* Create Time		: 2012-05-16 
	* Modify Time		: 2012-05-16
	* Modify Profile	:  
	******************************************************/
	public function del($id = null) 
	{
		//$id = isset($_GET['id'])?$_GET['id']:$id;
		//$this->page_class_manager->delete($id); 
		//$this->goto_last_index_page();
		
		$id = isset($_GET['id'])?$_GET['id']:$id;
		if(!empty($id) && $id!=1)
		{
			$this->page_class_manager->delete($id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $id)
			{
				if( $id!=1){
					$this->page_class_manager->delete($id) ;
				}
			}
		}
		$this->goto_last_index_page();
	}
	
	public function get_list_for_ztree()
	{
		$id = isset($_REQUEST['id'])?$_REQUEST['id']:0;
		//echo $id;exit;
		//$lists = "{id:'',name:'No Data',isParent:'false'}";
		$lists = Array();
		if($id==0)
		{ 
			$lists = $this->page_class_manager->get_root_list();
		}
		else
		{
			$lists = $this->page_class_manager->get($id)->children;
		}
		
		$list_jsons = array();
		foreach ($lists as $acls)
		{
			$isParent = sizeof($acls->children)==0?'false':'true';
			$list_jsons[] = "{id:'$acls->id',name:'$acls->title',isParent:'$isParent'}" ;
		}
		
		if(sizeof($list_jsons)==0)
		{
			echo "no data";
		}
		else
		{
			echo "[".implode(',',$list_jsons)."]";
		}
	}
	
	/******************************************************
	 * Profile		: 排序
	* Author		: 宁秀娟
	* Create Time		: 2012-05-14
	* Modify Time		: 2012-05-14
	* Modify Profile	:
	******************************************************/
	public function order()
	{
		// print_r($_POST['listorder']);exit;
		if(isset($_POST['listorder']) && is_array($_POST['listorder']))
		{
			foreach($this->input->post('listorder') as $k=>$v)
			{
				$this->page_class_manager->update(array('order'=>$v),array('id'=>$k)) ; 
			}
		}
		$this->goto_last_index_page();
	}
	/******************************************************
	 * Profile		: 移动到
	* Author		: 宁秀娟
	* Create Time		: 2012-06-08
	* Modify Time		: 2012-06-08
	* Modify Profile	:
	******************************************************/
	public function moveto($parent_id,$id=null)
	{
		$id = isset($_GET['id'])?$_GET['id']:$id;
		if(!empty($id))
		{
			$this->page_class_manager->moveto($parent_id,$id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $id)
			{
				// echo $article_id . "-$class_id";
				$this->page_class_manager->moveto($parent_id,$id) ;
			}
		}
		$this->goto_last_index_page();
	}
}

